package com.iespring.Scheduling;

import com.iespring.mapper.admin.ConfigSystemMapper;
import com.iespring.util.configmanager.ConfigKeyValue;
import com.iespring.util.configmanager.ConfigManagerUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 定时任务
 * @param
 * @return
 * @author 杨宇健
 * @since 2022/12/30 23:05
 */

@Component
public class Scheduling {
    @Resource
    ConfigSystemMapper configSystemMapper;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //每隔5秒执行一次该方法
    //@Scheduled(cron = "*/5 * * * * ?")
    //每分钟执行一次该方法
    //@Scheduled(cron = "0 */1 * * * ?")
    //public void testScheduleTask() {
    //    System.out.println("SpringBoot的定时任务" + Thread.currentThread().getName() + sdf.format(new Date()));
    //}
    //@Scheduled(cron = "*/5 * * * * ?")
    //每分钟执行一次
    @Scheduled(cron = "0 */1 * * * ?")
    public void unpaidTimeOut(){
        List<ConfigKeyValue> configList = configSystemMapper.selectOrder();
        Map<String, String> configMap = ConfigManagerUtils.listToMap(configList);
        //确认收货后超期 天 取消评论资格 401, 402 ->
        int commentTimeOut = Integer.parseInt(configMap.get("market_order_comment"));
        //订单发货后超期 天 系统自动收货  301 -> 402
        int unconfirmTimeOut = Integer.parseInt(configMap.get("market_order_unconfirm"));
        //用户下单后超时 分 系统自动取消  101 -> 103
        int unpaidTimeOut = Integer.parseInt(configMap.get("market_order_unpaid"));

        //用户下单后超时 101 -> 103
        List<Integer> unpaidIds = configSystemMapper.selectByTimeAndStatus(unpaidTimeOut, 101);
        if (unpaidIds.size() == 0) {
            System.out.println("本次扫描未发现超时未支付订单");
            return;
        }
        int unpaid = configSystemMapper.updateOrderStatus(103, unpaidIds);
        System.out.println("本次扫描处理了 " + unpaid + " 超时未支付份订单");




        //configSystemMapper.selectByTimeAndStatus()

    }

    //每天扫描一次
    @Scheduled(cron = "0 0 0 * * ?")
    public void unconfirmTimeOut(){
        List<ConfigKeyValue> configList = configSystemMapper.selectOrder();
        Map<String, String> configMap = ConfigManagerUtils.listToMap(configList);
        //确认收货后超期 天 取消评论资格 401, 402 ->
        int commentTimeOut = Integer.parseInt(configMap.get("market_order_comment"));
        //订单发货后超期 天 系统自动收货  301 -> 402
        int unconfirmTimeOut = Integer.parseInt(configMap.get("market_order_unconfirm"));
        //用户下单后超时 分 系统自动取消  101 -> 103
        int unpaidTimeOut = Integer.parseInt(configMap.get("market_order_unpaid"));

        //TODO 解除注释
        //订单发货后超期 天 系统自动收货  301 -> 402
        List<Integer> status = new ArrayList<>();
        status.add(301);
        List<Integer> unconfirmIds = configSystemMapper.selectByDayAndStatus(unconfirmTimeOut, status);
        if (unconfirmIds.size() == 0) {
            System.out.println("本次扫描未发现超时未确认收货订单");
            return;
        }
        int unconfirm = configSystemMapper.updateOrderStatus(402, unconfirmIds);
        System.out.println("本次扫描处理了 " + unconfirm + " 超时未确认收货订单");
    }
}
